#ifndef __BEZIER_H__
#define __BEZIER_H__

#include "Bernstein.h"
#include "Angles.h"

class Bezier{
 public:
	Bezier(int bezier_segments, double *controlpoints, int points_to_evaluate, Degrees angle_increment) {
		if (bezier_segments == 0)
			throw "LOL";

		vertices_height = points_to_evaluate;
		vertices_width = 0;
		for (double angle=0; angle<360; angle+=angle_increment.degrees) {
			vertices_width++;
		}

		texcoords = (double*)malloc(sizeof(double)*bezier_segments*3*vertices_width*vertices_height);		
		vertices  = (double*)malloc(sizeof(double)*bezier_segments*3*vertices_width*vertices_height);
		//		indices   = (double*)malloc(sizeof(double)*bezier_segments*4*vertices_width*vertices_height);

		Vector4 p0(controlpoints[0], controlpoints[1], 0, 1);
		Vector4 p1(controlpoints[2], controlpoints[3], 0, 1);
		Vector4 p2(controlpoints[4], controlpoints[5], 0, 1);
		Vector4 p3(controlpoints[6], controlpoints[7], 0, 1);

		int controlpoints_used = 4;
#ifdef MATLAB_OUTPUT
		cout << "close all; clear all; "<< endl;
		cout << "figure; hold on" << endl;
#endif
		int i=0;
		int j=0;

		int segment=1;
		while (controlpoints_used <= 3*(bezier_segments-1)+4) { 
			Bernstein bern(p0,p1,p2,p3);
			double t=0.0;
			for (int current_point=0; current_point<points_to_evaluate; current_point++) {

				Vector4 p = bern.evaluate(t); // Point evaluated at curve.
				t+=1.0/points_to_evaluate;

				Matrix4 rot;

				for (double angle=0; angle<360; angle+=angle_increment.degrees) {
					rot.rotateY_degrees(angle);
					Vector4 rotated=rot*p;
					vertices[i++] = rotated[0];
					vertices[i++] = -rotated[1];
					vertices[i++] = rotated[2];
					
					texcoords[j++] = angle/360.0;
					texcoords[j++] = t*segment;
					texcoords[j++] = 0;
				}
			}
			
			p0=p3;

			p1 = Vector4(controlpoints[2*controlpoints_used], controlpoints[2*controlpoints_used+1], 0, 1);
			controlpoints_used++;

			p2 = Vector4(controlpoints[2*controlpoints_used], controlpoints[2*controlpoints_used+1], 0, 1);
			controlpoints_used++;

			p3 = Vector4(controlpoints[2*controlpoints_used], controlpoints[2*controlpoints_used+1], 0, 1);
			controlpoints_used++;

			segment++;
		}

	}
	
	int vertices_width;
	int vertices_height;
	double *vertices;
	double *normals;
	double *texcoords;
	double *indices;

 private:


};



#endif
